﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Text;

namespace MyPush.Client
{


    /// <summary>
    /// 聚合云推服务.
    /// </summary>
    public class JuhePushService
    {

        private const string JUHE_URL = "https://tui.juhe.cn/api/plus/pushApi";


        private string _token;

        private string _service_id;


        /// <summary>
        /// 构造函数.
        /// </summary>
        /// <param name="token">聚合云推的 toekn.</param>
        /// <param name="serviceid">聚合云推的 service_id.</param>
        public JuhePushService(string token,  string serviceid)
        {
            _token = token;
            _service_id = serviceid;
        }



        /// <summary>
        /// 推送.
        /// </summary>
        /// <param name="title">推送标题</param>
        /// <param name="content">推送内容</param>
        /// <returns></returns>
        public string Push(string title, string content)
        {
            string sourceText = $"token={_token}&service_id={_service_id}&title={title}&content={content}";
            byte[] postData = Encoding.UTF8.GetBytes(sourceText);
            using (WebClient webClient = new WebClient())
            {

                webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

                // 得到返回字符流  
                byte[] responseData = webClient.UploadData(JUHE_URL, "POST", postData);
                // 解码
                string resultString = Encoding.UTF8.GetString(responseData);

                return resultString;
            }            
        }



    }
}
